package com.trace.util;

import java.util.concurrent.Semaphore;

/**
 * Created by denglw on 2020/1/9.<br/>
 * Desc: 信号量
 */
public class SemaphoreTest {

    private static final int THREAD_COUNT = 30;
    private static Semaphore s = new Semaphore(10);

    public static void main(String[] args) {
        for (int i = 0; i < THREAD_COUNT; i++) {
            new Thread(() -> {
                try {
                    s.acquire();
                    System.out.println("save data");
                    Thread.sleep(5000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    s.release();
                }
            }).start();
        }
    }

}
